import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from '@/router/index'
import axios from 'axios'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import locale from "element-plus/es/locale/lang/zh-cn";
import iconsSvg from "@/plugins/iconsSvg.js";
import Mui from "@/plugins/mui.min.js";
import { createPinia } from 'pinia';
import piniaPluginPersist from 'pinia-plugin-persist';
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
import Vant, { Lazyload } from 'vant';
import 'vant/lib/index.css';
import "amfe-flexible"
import BaiduMap from 'vue-baidu-map-3x'


const app = createApp(App)
const pinia = createPinia();

// 全局注册el-icon
for (const [name, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(name, component);
}
// app.use(ElementPlus)
app.use(ElementPlus, { locale })
app.use(router)
app.use(iconsSvg)
app.use(pinia)
app.use(Vant)
app.use(Lazyload, {
  preLoad: 1.3,
  error: 'error.png',
  loading: 'loading.jfif',
  attempt: 1,
});
app.use(Mui)
pinia.use(piniaPluginPersist);
app.use(VXETable)
app.use(BaiduMap, {
  ak: 'hVFrXmwrn1aFlaHG1SXqmMobVZB3RSr0'
})

app.config.globalProperties.$http = axios


import { showToast } from 'vant';
import { useStore } from "@/store";


const store = useStore();
const userinfo = store.getUserStoreinfo;
// 8.130.164.140,120.55.61.102
if (userinfo !== null && userinfo !== undefined) {
  const userid = userinfo.id
  let tablewebsocketURL = `ws://8.130.164.140:8070/table/user${userid}/`;
  const websocket = new WebSocket(tablewebsocketURL);

  // 全局监听WebSocket消息
  websocket.onmessage = (event) => {
    let data = JSON.parse(event.data)
    data.isresd = false;
    const tablemessage = store.getTableMessage
    tablemessage.push(data)
    store.setTableMessage(tablemessage)
    // 这里可以实现一些全局的逻辑，比如更新状态管理库(Vuex, Pinia)的状态
  };
  app.config.globalProperties.$websocket = websocket;
}

// document.addEventListener('plusready', function () {
//   var first = null;
//   var webview = plus.webview.currentWebview();
//   plus.key.addEventListener('backbutton', function () {
//     webview.canBack(function (e) {
//       if (e.canBack) {
//         webview.back(); //这里不建议修改自己跳转的路径  
//       } else {
//         //首次按键，提示‘再按一次退出应用’  
//         if (!first) {
//           first = new Date().getTime(); //获取第一次点击的时间戳  
//           plus.nativeUI.toast("再按一次退出应用", {
//             duration: 'short'
//           }); //通过H5+ API 调用Android 上的toast 提示框  
//           setTimeout(function () {
//             first = null;
//           }, 1000);
//         } else {
//           // 获取第二次点击的时间戳, 两次之差 小于 1000ms 说明1s点击了两次, 
//           if (new Date().getTime() - first < 1000) {
//             plus.runtime.quit(); //退出应用  
//           }
//         }
//       }
//     });
//   });
// });

Mui.init({
  keyEventBind: {
    backbutton: true, //关闭back按键监听
  },
});
//首页返回键处理
// 处理逻辑： 1s内，连续两次按返回键，则退出应用；
var first = null;
Mui.back = function () {
  if (sessionStorage.getItem("quit") == 1) {
    // 首次按键， 提示 再按一次退出应用
    if (!first) {
      first = new Date().getTime(); //记录第一次按下回退键的时间
      Mui.toast("再按一次退出应用");
      setTimeout(function () {
        //1s 后清除
        first = null;
      }, 1000);
    } else {
      if (new Date().getTime() - first < 1000) {
        //如果两次按下的时间小于1s
        plus.runtime.quit(); //那么就退出app
      }
    }
  } else {
    history.go(-1); // 回退到上一页
  }
};





router.beforeEach(async (to, from, next) => {
  if (to.meta.isAuth) {
    const userinfo = await store.getUserStoreinfo;
    if (userinfo !== null && userinfo !== undefined) {
      next()
    } else {
      showToast({
        message: '您的登录已失效或未登录，请重新登录\n即将跳转到登录页面',
        position: 'bottom',
        duration: 3000
      });
      setTimeout(() => {
        next('/')
      }, 3000);
    }
  } else {
    next()
  }
})

app.mount('#app')